#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :save_new_movie.py
# @Time      :2023/10/12 
# @Author    :CL
# @email     :1037654919@qq.com
import datetime

import pandas
from bs4 import BeautifulSoup

from douban import douban


# 豆瓣新片榜 一周口碑榜 北美票房榜
def save_new_movie():
    response = douban.get_new_movies()
    soups = BeautifulSoup(response.text, 'lxml')
    # 豆瓣新片榜
    datas = soups.find('div', class_='article').find_all('table')
    lists = []
    for data in datas:
        pl2 = data.find('div', class_='pl2')
        # 解析
        name = str(pl2.find('a').get_text()).strip()
        href = pl2.find('a')['href']
        info = pl2.find('p').get_text().strip()
        rating_nums = pl2.find('span', class_="rating_nums").get_text()
        comment = pl2.find('span', class_="pl").get_text()

        lists.append([name, href, info, rating_nums, comment])
    pf = pandas.DataFrame(lists)
    pf.to_excel(f'豆瓣新片榜_{datetime.datetime.today().strftime("%Y%m%d")}.xlsx')

    # 一周口碑榜
    datas = soups.find('div', class_='movie_top').find_all('div', class_='movie_top')[0]
    print(datas)
    times = str(datas.find('h2').find('span').text).split(' ')[0]
    lis = datas.find_all('li')
    lists = []
    for li in lis:
        num = li.find('div', class_='no').text
        name = li.find('div', class_='name').find('a').text.strip()
        href = li.find('div', class_='name').find('a')['href']
        gengxin = li.find('span', class_="").find('div')['class']
        gengxin_num = li.find('span', class_="").find('div').text
        lists.append([num, name, href, gengxin, gengxin_num])
    pf = pandas.DataFrame(lists)
    pf.to_excel(f'一周口碑榜_{times}.xlsx')

    # 北美票房榜
    datas = soups.find('div', class_='movie_top').find_all('div', class_='movie_top')[1]
    # print(datas)
    times = str(datas.find('h2').find('span').text).split(' ')[0]
    lis = datas.find_all('li')
    lists = []
    for li in lis:
        num = li.find('div', class_='no').text
        name = li.find('div', class_='box_chart').find('a').text.strip()
        href = li.find('div', class_='box_chart').find('a')['href']
        box_chart_num = li.find('span', class_='box_chart_num').text
        lists.append([num, name, href, box_chart_num])
    pf = pandas.DataFrame(lists)
    pf.to_excel(f'北美票房榜_{times}.xlsx')


if __name__ == '__main__':
    save_new_movie()
